var productsArray = new Array();
var asc=true;

function displayProducts()
{


	if (productsArray.length>0)
	{
	var output='<table><caption> Results </caption><tr><th><a href="" onClick="javascript:sortBy(\'name\')">Name</a></th><th>Category</th><th>Picture</th><th>Price</th></tr>';
	for(w=0; w<productsArray.length; w++)
	{
		output+='<tr><th>'+productsArray[w]['prod_name']+'</th><td>'+productsArray[w]['category']+'</td><td><img src="'+productsArray[w]['img']+'"/></td><td> '+productsArray[w]["price"]+'$ </td></tr>';
	}
	output += '</table>';
	}
	else
	{
		          	output='<p><span class="error">No product found</span></p>';

	}
	
	document.getElementById('products').innerHTML=output;
}

function sortBy(type)
{
	
	switch (type) 
		{ 
		case 'name': 
			productsArray.sort(sortByName);
			displayProducts();
		break; 
		default: 
			productsArray.sort(sortByRank);
			displayProducts();
		break; 
		}
	
	
	
	
}



function sortByRank(a, b){
	asc=!asc;
	if(asc)
	{
return a.rank-b.rank;
	}
	else
	{
return b.rank-a.rank;
	}
}

function sortByName(a, b){
	asc=!asc;
	if(asc)
	{
return a.prod_name-b.prod_name;
	}
	else
	{
return b.prod_name-a.prod_name;
	}
}


function getCatNames()
{

 var language_id = 1
	var myAjax = new Ajax.Request(
    'http://eiffel.itba.edu.ar/hci/service/Catalog.groovy',
    {

      method: 'get',
      parameters : {
					method : 'GetCategoryList',
					language_id : language_id
				},

      onSuccess: function (xhr)
        {

          // Objet Document contenu dans la propriété responseXML
          var node = xhr.responseXML;

          var nodes = node.firstChild.getElementsByTagName('category');

			
          for (q=0 ; q<nodes.length ; q++)
          {
	
            var node = nodes[q];
 		    
 		   // var nodeId = node.getElementsByTagName('id')[0];
          //  var id = nodeId.firstChild.nodeValue;
            var attributes = node.attributes;
            var id = attributes.getNamedItem('id').nodeValue;
			var nodeCatName = node.getElementsByTagName('name')[0];
            var catName = nodeCatName.firstChild.nodeValue;
            			

			for(j=0; j<productsArray.length; j++)
			{
				
				if(productsArray[j]['category']==id)
				{
				productsArray[j]['category']=catName;
				}
			}
			
				productsArray.sort(sortByRank)
						displayProducts();

             			
         	}
        } // onSuccess
    }
  );
}




function searchIt(query)
{
	
	productsArray=[];
	document.getElementById('products').innerHTML='<img src="imagenes/ajax-loader.gif" alt="Loading">';
	
	var myAjax = new Ajax.Request(
    'http://eiffel.itba.edu.ar/hci/service/Catalog.groovy',
    {

      method: 'get',
      parameters : {
					method : 'GetProductListByName',
					criteria : query
				},

      onSuccess: function (xhr)
        {

          // Objet Document contenu dans la propriété responseXML
          var node = xhr.responseXML;

          var nodes = node.firstChild.getElementsByTagName('product');
          var output = '';
          


          for (i=0 ; i<nodes.length ; i++)
          {
					
					
					
            var node = nodes[i];
 		    var tempArray = new Array();
 		    
 		    
 		    var attributes = node.attributes;
            var id = attributes.getNamedItem('id').nodeValue;
            tempArray["id"] = id;
 		    
 		    
 		   // var nodeId = node.getElementsByTagName('id')[0];
          //  var id = nodeId.firstChild.nodeValue;
            var nodeCat = node.getElementsByTagName('category_id')[0];
            var cat = nodeCat.firstChild.nodeValue;
            tempArray["category"] = cat;
            
            
            var nodeSubCat = node.getElementsByTagName('subcategory_id')[0];
            var subCat = nodeSubCat.firstChild.nodeValue;
            tempArray["subcategory"] = subCat;

            
            var nodeProdName = node.getElementsByTagName('name')[0];
            var prodName = nodeProdName.firstChild.nodeValue;
            tempArray["prod_name"] = prodName;

            var nodeRank = node.getElementsByTagName('sales_rank')[0];
            var rank = nodeRank.firstChild.nodeValue;
            tempArray["rank"] = rank;

            
            
            var nodePrice = node.getElementsByTagName('price')[0];
            var price = nodePrice.firstChild.nodeValue;
            tempArray["price"] = price;


            var nodeImg = node.getElementsByTagName('image_url')[0];
            var img = nodeImg.firstChild.nodeValue;
            tempArray["img"] = img;


			productsArray.push(tempArray);

  			//	output += '<div class="product">'+'<a href="#" class="cat'+cat+'">'+'</a>><a href="#" class="cat'+cat+'subcat'+subCat+'">'+'</a><br/>'+'<a href="#">'+prodName+'</a><br/>'+'<img src="'+img+'" alt="Imagen" /><br/>'+'<em>'+price+' $</em><br/>';
  			
  			
                   }
                   

			//document.getElementById('products').innerHTML=output;
			getCatNames();
        } // onSuccess
    }
  );
	
}



